import { error } from 'console'
import {
  Write_player,
  Write_cangku,
  fs,
  _path,
  _item,
  _dirname,
  path
} from '../../mode/allapi'
import {  RedisA} from '../../api/api'
import axios from 'axios'
export const player = {
  name: '',
  user_id: '',
  ConstructionQuantity: 1, 
  food: '草场、粮草',
  food_level: 0, 
  food_production: 500, 
  food_num: 5000, 
  food_max: 10000, 
  stone: '采石场，石料',
  stone_level: 0, 
  stone_production: 500, 
  stone_num: 5000, 
  stone_max: 10000, 
  wood: '伐木场、木材',
  wood_level: 0, 
  wood_production: 500, 
  wood_num: 5000,
  wood_max: 10000, 
  iron: '冶铁台、铁矿',
  iron_level: 0, 
  iron_production: 500,
  iron_num: 5000, 
  iron_max: 10000,
  currency: '工坊、五铢钱',
  currency_level: 0, 
  currency_production: 0, 
  currency_num: 0, 
  warehouse: '仓库',
  warehouse_level: 0, 
  DungeonStorey: 0, 
  militaryNum: 0, 
  position: 0, 
  havecity: 0, 
  time: 0, 
  state: 0,
  garrisons: 0,
  wujian: [{}, {}, {}, {}, {}, {}, {}, {}, {}]
}

export const barracks = [
  {
    id: 1000,
    name: '士兵',
    type: '士兵',
    space: 1,
    price: 100,
    num: 500
  }
]

export const cangku = [
  {
    id: 50001,
    name: '普通招募令',
    type: '招募武将',
    price: 5000,
    effect: '用于招募二流武将和一流武将',
    num: 10
  }
]

export async function setplayer(id: string) {
  const name = await axios.get('http://81.70.100.130/api/xingming.php')
  player.time = new Date().getTime()
  player.name = name.data
  player.user_id = id
  await Write_player(id, player)
  await Write_cangku('cangku', id, cangku)
  await Write_cangku('barracks', id, barracks)
}

export async function delplayer(id: string) {
  fs.unlinkSync(
    path.join(_dirname, 'resources', 'data', 'player', `${id}.json`)
  )
  fs.unlinkSync(
    path.join(_dirname, 'resources', 'data', 'barracks', `${id}.json`)
  )
  fs.unlinkSync(
    path.join(_dirname, 'resources', 'data', 'cangku', `${id}.json`)
  )
  const path1 = path.join(_dirname, 'resources', 'data', 'item', 'place.json')
  const map: any = fs.readFileSync(path.join(path1), 'utf-8')
  for (let i = 0; i < map.length; i++) {
    if (map.belong == id) {
      map.belong = 0
      map.garrison = 0
    }
  }
  fs.writeFile(path1, map, error)
  const key = 'shengji:player:' + id + ':Paction'
  if (key) await RedisA.del(key)
}
export async function setredis(id: string) {
  const nowTime1 = 12 * 60 * 60
  const nowTime = new Date().getTime() 
  await RedisA.set(`age:time${id}:@1.2`, nowTime)
  await RedisA.expire(`age:time${id}:@1.2`, nowTime1)
}
